﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web.Controllers
{
    public class HistoryController : CustomBaseController
    {
        //
        // GET: /History/

        [HttpPost]
        public ViewResult Resentmessage(int Id, int Batchid)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            dataAccess.ResentMessageById(Id);
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            HistoryModel model = new HistoryModel();
            model.Infos = dataAccess.GetHistoryByAll(1, maxshow, ViewHistoryType.failed, "", Batchid);
            model.BatchId = Batchid;
            return View("historygrid", model);
        }



        [HttpPost]
        public ViewResult ResentOrDeleteMessages(string[] item, int Batchid, string todo)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);


            if (item != null && item.Length != 0)
            {
                if (todo == "重发" || todo == "resent")
                {
                    foreach (var id in item)
                        dataAccess.ResentMessageById(Convert.ToInt32(id));
                }
                else
                {
                    foreach (var id in item)
                        dataAccess.DeleteMessageById(Convert.ToInt32(id));
                }
            }
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            HistoryModel model = new HistoryModel();
            model.Infos = dataAccess.GetHistoryByAll(1, maxshow, ViewHistoryType.failed, "", Batchid);
            model.BatchId = Batchid;
            return View("historygrid_failed", model);
        }

        [HttpPost]
        public ViewResult ForwardOrDeleteMessages(string[] item, int Batchid, string todo)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);


            if (item != null && item.Length != 0)
            {
                if (todo == "删除" || todo == "delete")
                {
                    //    foreach (var id in item)
                    //        dataAccess.ResentMessageById(Convert.ToInt32(id));
                    //}
                    //else
                    //{
                    foreach (var id in item)
                        dataAccess.DeleteMessageById(Convert.ToInt32(id));
                }
            }
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            HistoryModel model = new HistoryModel();
            model.Infos = dataAccess.GetHistoryByAll(1, maxshow, ViewHistoryType.failed, "", Batchid);
            model.BatchId = Batchid;
            return View("historygrid_delivered", model);
        }

        //[HttpPost]
        //public ViewResult ForwardMessage(int Id)
        //{
        //    WebConfigProvider configProvider = new WebConfigProvider();
        //    IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
        //   dataAccess.GetMessageContentBy
        //    return View("historygrid_delivered");
        //}

        [HttpPost]
        public ViewResult Deletemessage(int Id, int Batchid)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            dataAccess.DeleteMessageById(Id);
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            HistoryModel model = new HistoryModel();
            model.Infos = dataAccess.GetHistoryByAll(1, maxshow, ViewHistoryType.failed, "", Batchid);
            model.BatchId = Batchid;
            return View("historygrid_failed", model);
        }

        public ActionResult Index()
        {
            return View();
        }

        #region Failed
        public ViewResult Failed(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";

            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Failed_Group(Model);
                case "company":
                    return Failed_Company(Model);
                default:
                    return Failed_Self(Model);
            }
        }


        public ViewResult Failed_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, 2, ViewHistoryType.failed, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.User, 2, maxshow, Model.key, Model.BatchId);
            return View("Failed_Self", Model);
        }

        public ViewResult Failed_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.failed, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Failed_Group", Model);
        }

        private ViewResult Failed_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.failed, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.failed, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Failed_Company", Model);
        }
        #endregion

        #region Delivered
        public ViewResult Delivered(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";

            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Delivered_Group(Model);
                case "company":
                    return Delivered_Company(Model);
                default:
                    return Delivered_Self(Model);
            }
        }


        public ViewResult Delivered_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, 2, ViewHistoryType.delivered, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.User, 2, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Self", Model);
        }

        public ViewResult Delivered_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.delivered, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Group", Model);
        }

        private ViewResult Delivered_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.delivered, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.delivered, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Delivered_Company", Model);
        }
        #endregion


        #region Pending

        public ViewResult Pending(HistoryModel Model)
        {
            if (Model.page == 0)
            {
                Model.page = 1;
            }

            if (Model.category == null)
                Model.category = "";

            switch (Model.category.ToLower())
            {
                //case "group":
                //    return Pending_Group(Model);
                case "company":
                    return Pending_Company(Model);
                default:
                    return Pending_Self(Model);
            }
        }


        public ViewResult Pending_Self(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByUser(Model.page, maxshow, 2, ViewHistoryType.pending, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.User, 2, maxshow, Model.key, Model.BatchId);
            return View("Pending_Self", Model);
        }

        public ViewResult Pending_Group(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByGroup(Model.page, maxshow, 1, ViewHistoryType.pending, Model.key);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.Group, 1, maxshow, Model.key, Model.BatchId);
            return View("Pending_Group", Model);
        }

        private ViewResult Pending_Company(HistoryModel Model)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            int maxshow = Convert.ToInt32(configProvider.MaxshowInHistory);
            IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
            Model.Infos = dataAccess.GetHistoryByAll(Model.page, maxshow, ViewHistoryType.pending, Model.key, Model.BatchId);
            Model.PageCount = dataAccess.GetPageCount(ViewHistoryType.pending, ViewHistoryRole.All, 0, maxshow, Model.key, Model.BatchId);
            return View("Pending_Company", Model);
        }

        #endregion
        //public ActionResult ResentByBatchId(HistoryModel Model)
        //{
        //    WebConfigProvider configProvider = new WebConfigProvider();
        //    IHistoryWebDataAccess dataAccess = new sqlReportWebDataAccess(configProvider.ConnectionString);
        //    dataAccess.ResentMessage(Model.BatchId);
        //    return RedirectToAction("failed", "history", new { pageindex = 1 , batchid = Model.BatchId});
        //}

    }
}
